package com.ZengQi.event.view;

import com.ZengQi.event.controller.LoginController;
import com.ZengQi.event.entity.Admin;
import com.ZengQi.event.entity.Publisher;
import com.ZengQi.event.entity.User;


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;

public class LoginView extends JFrame implements ActionListener {

    JLabel nameLabel = new JLabel("登录界面", JLabel.CENTER);
    SpringLayout springLayout = new SpringLayout();
    JPanel centerPanel = new JPanel(springLayout);

    JLabel userNameLabel = new JLabel("手机号");
    JTextField userTxt = new JTextField();
    JLabel pwdLabel = new JLabel("密码");
    JPasswordField pwdField = new JPasswordField();
    JButton loginBtn = new JButton("登录");
    JButton resetBtn = new JButton("重置");
    JButton regisBtn=new JButton("注册");
    JRadioButton userRadioBtn=new JRadioButton("用户");
    JRadioButton adminRadioBtn=new JRadioButton("企业管理员");
    JRadioButton publisherRadioBtn =new JRadioButton("企业人员");


    SystemTray systemTray;
    TrayIcon trayIcon;

    LoginController loginController;

    //登录界面的初始化
    public LoginView() {
        super("做题帮");


        loginController=new LoginController(this);
        Container contentPane = getContentPane();
        //字体设置
        Font centerFont = new Font("楷体", Font.PLAIN, 20);
        nameLabel.setFont(new Font("华文行楷", Font.PLAIN, 40));
        nameLabel.setPreferredSize(new Dimension(0, 80));
        userNameLabel.setFont(centerFont);
        userTxt.setPreferredSize(new Dimension(200, 30));

        pwdLabel.setFont(centerFont);
        pwdField.setPreferredSize(new Dimension(200, 30));


        resetBtn.setFont(centerFont);
        loginBtn.setFont(centerFont);
        regisBtn.setFont(centerFont);
        adminRadioBtn.setFont(centerFont);
        userRadioBtn.setFont(centerFont);
        publisherRadioBtn.setFont(centerFont);


        centerPanel.add(userNameLabel);
        centerPanel.add(userTxt);
        centerPanel.add(pwdLabel);
        centerPanel.add(pwdField);
        centerPanel.add(regisBtn);
        centerPanel.add(adminRadioBtn);
        centerPanel.add(publisherRadioBtn);
        centerPanel.add(userRadioBtn);

        ButtonGroup buttonGroup=new ButtonGroup();
        buttonGroup.add(adminRadioBtn);
        buttonGroup.add(publisherRadioBtn);
        buttonGroup.add(userRadioBtn);

        //增加按键事件监听
        loginBtn.addActionListener(this);





        centerPanel.add(loginBtn);
        resetBtn.addActionListener(this);
        centerPanel.add(resetBtn);

        regisBtn.addActionListener(this);
        centerPanel.add(regisBtn);

        contentPane.add(nameLabel, BorderLayout.NORTH);
        contentPane.add(centerPanel, BorderLayout.CENTER);


        latoutCenter();


        if (SystemTray.isSupported()) {
            systemTray = SystemTray.getSystemTray();
            URL imgurl = LoginView.class.getClassLoader().getResource("bang.png");
            trayIcon = new TrayIcon(new ImageIcon(imgurl).getImage());
            //设置图片大小自动缩放
            trayIcon.setImageAutoSize(true);

            try {
                systemTray.add(trayIcon);
            } catch (AWTException e) {
                e.printStackTrace();
            }

            //最小化时销毁资源
            this.addWindowListener(new WindowAdapter() {
                @Override
                public void windowIconified(WindowEvent e) {
                    LoginView.this.dispose();
                }
            });
            //托盘事件监听
            trayIcon.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    int clickCount =e.getClickCount();
                    if(clickCount==1){
                        LoginView.this.setExtendedState(JFrame.NORMAL);
                    }
                    LoginView.this.setVisible(true);
                }
            });
            //设置默认按钮loginbtn
            getRootPane().setDefaultButton(loginBtn);



            //自定义缩放图标
            URL resource = LoginView.class.getClassLoader().getResource("bang.png");
            Image image = new ImageIcon(resource).getImage();
            setIconImage(image);


            setSize(600, 400);//单位是像素
            setVisible(true);//使界面可视化
            //居中
            setLocationRelativeTo(null);
            //关闭时退出程序
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //大小不可改变
            setResizable(false);
        }


    }

    /**
     *
     *
     */
    /**
     * 登录按钮的事件监听
     * @param e
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jButton= (JButton) e.getSource();
        String text =jButton.getText();
        if(!userRadioBtn.isSelected()&&!adminRadioBtn.isSelected()&&!publisherRadioBtn.isSelected()){
            JOptionPane.showMessageDialog(null, "请选择你的身份！");
            return;
        }
        if("重置".equals(text)){
            getUserTxt().setText("");
            getPwdField().setText("");
        }else if ("登录".equals(text)&&userRadioBtn.isSelected()){
            User conditionUser=new User();
            conditionUser.setLoginPnum(getUserTxt().getText());
            conditionUser.setLoginPwd(new String(getPwdField().getPassword()));
            loginController.login(conditionUser);

        }else if("注册".equals(text)){
            new RegisView();
            dispose();
        }else if("登录".equals(text)&&adminRadioBtn.isSelected()){
            Admin conditionAdmin=new Admin();
            conditionAdmin.setLoginPnum(getUserTxt().getText());
            conditionAdmin.setLoginPwd(new String(getPwdField().getPassword()));
            loginController.login(conditionAdmin);
        }else if("登录".equals(text)&&publisherRadioBtn.isSelected()){
            Publisher conditionPublisher=new Publisher();
            conditionPublisher.setLoginPnum(getUserTxt().getText());
            conditionPublisher.setLoginPwd(new String(getPwdField().getPassword()));
            loginController.login(conditionPublisher);
        }
    }

    /**
     * 登录界面的布局
     */
    private void latoutCenter() {
        Spring childWidth = Spring.sum(Spring.sum(Spring.width(userNameLabel), Spring.width(userTxt)), Spring.constant(20));
        int offsetX = childWidth.getValue() / 2;
        springLayout.putConstraint(SpringLayout.WEST, userNameLabel, -offsetX, SpringLayout.HORIZONTAL_CENTER, centerPanel);
        springLayout.putConstraint(SpringLayout.NORTH, userNameLabel, 40, SpringLayout.NORTH, centerPanel);

        springLayout.putConstraint(SpringLayout.WEST, userRadioBtn, 0, SpringLayout.WEST, userNameLabel);
        springLayout.putConstraint(SpringLayout.NORTH, userRadioBtn, -35, SpringLayout.NORTH, userNameLabel);

        springLayout.putConstraint(SpringLayout.WEST, adminRadioBtn, 20, SpringLayout.EAST, userRadioBtn);
        springLayout.putConstraint(SpringLayout.NORTH, adminRadioBtn, 0, SpringLayout.NORTH, userRadioBtn);

        springLayout.putConstraint(SpringLayout.WEST, publisherRadioBtn, 20, SpringLayout.EAST, adminRadioBtn);
        springLayout.putConstraint(SpringLayout.NORTH, publisherRadioBtn, 0, SpringLayout.NORTH, adminRadioBtn);


        springLayout.putConstraint(SpringLayout.WEST, userTxt, 20, SpringLayout.EAST, userNameLabel);
        springLayout.putConstraint(SpringLayout.NORTH, userTxt, 0, SpringLayout.NORTH, userNameLabel);

        springLayout.putConstraint(SpringLayout.EAST, pwdLabel, 0, SpringLayout.EAST, userNameLabel);
        springLayout.putConstraint(SpringLayout.NORTH, pwdLabel, 20, SpringLayout.SOUTH, userNameLabel);

        springLayout.putConstraint(SpringLayout.WEST, pwdField, 20, SpringLayout.EAST, pwdLabel);
        springLayout.putConstraint(SpringLayout.NORTH, pwdField, 0, SpringLayout.NORTH, pwdLabel);

        springLayout.putConstraint(SpringLayout.WEST, loginBtn, 0, SpringLayout.WEST, pwdLabel);
        springLayout.putConstraint(SpringLayout.NORTH, loginBtn, 20, SpringLayout.SOUTH, pwdLabel);

        springLayout.putConstraint(SpringLayout.WEST, resetBtn, 120, SpringLayout.EAST, loginBtn);
        springLayout.putConstraint(SpringLayout.NORTH, resetBtn, 0, SpringLayout.NORTH, loginBtn);

        springLayout.putConstraint(SpringLayout.WEST, regisBtn, 25, SpringLayout.EAST, loginBtn);
        springLayout.putConstraint(SpringLayout.NORTH, regisBtn, 0, SpringLayout.NORTH, loginBtn);

    }



    public JTextField getUserTxt() {
        return userTxt;
    }

    public JPasswordField getPwdField() {
        return pwdField;
    }

}